﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using TuyennvLibrary.ServerApi.Models;
using TuyennvLibrary.ServerApi.Controllers;
using TuyennvLibrary.Reflection;

namespace TuyennvLibrary.ServerApi.Sms
{
    public class ProcessContent
    {
        public MOLogModel mologModel = new MOLogModel();
        public mt_logModel mtlogModel = new mt_logModel();
      
        //public MsgContent process(MsgObject mo, HttpServerUtility Server, bool isdev)
        //{
        //    sms_classproccessModel smsclassModel = new sms_classproccessModel();
        //    sms_classproccess smsclass = smsclassModel.getBy(mo.command_code, mo.serviceid, mo.message);
        //    string classProcess = smsclass.class_proccess;
        //    if (!string.IsNullOrEmpty(classProcess))
        //    {
        //        mo.subcodes = smsclass.subcodes;
        //        Type myTypeObj;
        //        object myclass;
        //        MethodInfo myMethodInfo;
        //        try
        //        {
        //            if (!string.IsNullOrEmpty(smsclass.file_dll))
        //            {
        //                //Load 
        //                string pathDll = Server.MapPath(Config.DirFileDLL + smsclass.file_dll);
        //                var dll = Assembly.LoadFile(pathDll);
        //                myTypeObj = dll.GetType(classProcess);
        //            }
        //            else
        //            {
        //                myTypeObj = Type.GetType(classProcess);
        //            }
        //            myclass = Activator.CreateInstance(myTypeObj);
        //            MethodInfo[] methods = myTypeObj.GetMethods();
        //            myMethodInfo = myTypeObj.GetMethod("service");

        //            ParameterInfo[] MethodParameters = myMethodInfo.GetParameters();
        //            long moId = 0;
        //            if (!isdev)
        //            {
        //                moId = mologModel.Insert(mo);
        //                mo.moId = moId;
        //            }
        //            object[] objParameters = new object[] { mo };
        //            MsgContent content = (MsgContent)myMethodInfo.Invoke(myclass, objParameters);
        //            if (content != null && !isdev)
        //            {
        //                mtlogModel.Insert(moId, content);
        //            }
        //            return content;

        //        }
        //        catch (Exception ex)
        //        {
        //            throw ex;
        //        }
        //    }
        //    //Khoi tao doi tuong cua class
        //    //Xac dinh method
        //    return null;
        //}
        public MsgContent proccess(MsgObject mo, HttpRequest Request, HttpServerUtility Server, bool isdev)
        {
            SMSProcessClassModel smsclassModel = new SMSProcessClassModel();

            SMSProcessClass smsclass = smsclassModel.getBy(mo.command_code, mo.serviceid, mo.message);
            string classProcess = smsclass.class_proccess;
            if (!string.IsNullOrEmpty(classProcess))
            {
                mo.subcodes = smsclass.subcodes;
                Type myTypeObj;
                object myclass;
                MethodInfo myMethodInfo;
                try
                {
                    if (!string.IsNullOrEmpty(smsclass.file_dll))
                    {
                        //Load 
                        string pathDll = Server.MapPath("FileDLL/" + smsclass.file_dll);
                        var dll = Assembly.LoadFile(pathDll);
                        myTypeObj = dll.GetType(classProcess);
                    }
                    else
                    {
                        myTypeObj = Type.GetType(classProcess);
                    }
                    myclass = Activator.CreateInstance(myTypeObj);
                    MethodInfo[] methods = myTypeObj.GetMethods();
                    myMethodInfo = myTypeObj.GetMethod("service");

                    ParameterInfo[] MethodParameters = myMethodInfo.GetParameters();
                    object[] objParameters = new object[] { mo, Request };
                    MsgContent content = (MsgContent)myMethodInfo.Invoke(myclass, objParameters);
                    long moId = 0;
                    if (!isdev)
                    {
                        moId = mologModel.Insert(mo);
                        mo.moId = moId;
                    }
                    if (content != null && !isdev)
                    {
                        mtlogModel.Insert(moId, content);
                    }
                    return content;

                }
                catch (Exception ex)
                {
                    return new MsgContent();
                }
            }
            //Khoi tao doi tuong cua class
            //Xac dinh method
            return new MsgContent();
        }
        public object processPartner(MsgObject mo, HttpRequest Request, HttpServerUtility Server, bool isdev)
        {
            SMSProcessClassModel smsclassModel = new SMSProcessClassModel();

            SMSProcessClass smsclass = smsclassModel.getBy(mo.command_code, mo.serviceid, mo.message);
            string classProcess = smsclass.class_proccess;
            if (!string.IsNullOrEmpty(classProcess))
            {
                mo.subcodes = smsclass.subcodes;
                Type myTypeObj;
                object myclass;
                MethodInfo myMethodInfo;
                try
                {
                    if (!string.IsNullOrEmpty(smsclass.file_dll))
                    {
                        //Load 
                        string pathDll = Server.MapPath("FileDLL/" + smsclass.file_dll);
                        var dll = Assembly.LoadFile(pathDll);
                        myTypeObj = dll.GetType(classProcess);
                    }
                    else
                    {
                        myTypeObj = Type.GetType(classProcess);
                    }
                    myclass = Activator.CreateInstance(myTypeObj, (!isdev) ? 1 : 0);
                    MethodInfo[] methods = myTypeObj.GetMethods();
                    myMethodInfo = myTypeObj.GetMethod("service");

                    ParameterInfo[] MethodParameters = myMethodInfo.GetParameters();
                    object[] objParameters = new object[] { mo, Request };
                    var content = myMethodInfo.Invoke(myclass, objParameters);
                    //long moId = mologModel.Insert(mo);
                    //mtlogModel.Insert(moId, content);
                    return content;

                }
                catch (Exception ex)
                {
                    return new { code = Code.ERROR, message = ex.Message };
                }
            }
            //Khoi tao doi tuong cua class
            //Xac dinh method
            return new { code = Code.ERROR, message = "khong co class" };
        }
        public MsgContent processPartnerNew(MsgObject mo, HttpRequest Request, HttpServerUtility Server, bool isdev)
        {
            SMSProcessClassModel smsclassModel = new SMSProcessClassModel();

            SMSProcessClass smsclass = smsclassModel.getBy(mo.command_code, mo.serviceid, mo.message);
            string classProcess = smsclass.class_proccess;
            if (!string.IsNullOrEmpty(classProcess))
            {
                mo.subcodes = smsclass.subcodes;
                Type myTypeObj;
                object myclass;
                MethodInfo myMethodInfo;
                try
                {
                    if (!string.IsNullOrEmpty(smsclass.file_dll))
                    {
                        //Load 
                        string pathDll = Server.MapPath("FileDLL/" + smsclass.file_dll);
                        var dll = Assembly.LoadFile(pathDll);
                        myTypeObj = dll.GetType(classProcess);
                    }
                    else
                    {
                        myTypeObj = Type.GetType(classProcess);
                    }
                    myclass = Activator.CreateInstance(myTypeObj, (!isdev) ? 1 : 0);
                    MethodInfo[] methods = myTypeObj.GetMethods();
                    myMethodInfo = myTypeObj.GetMethod("service");

                    ParameterInfo[] MethodParameters = myMethodInfo.GetParameters();
                    object[] objParameters = new object[] { mo, Request };
                    MsgContent content = (MsgContent)myMethodInfo.Invoke(myclass, objParameters);
                    //long moId = mologModel.Insert(mo);
                    //mtlogModel.Insert(moId, content);
                    return content;

                }
                catch (Exception ex)
                {
                    return new MsgContent();
                }
            }
            //Khoi tao doi tuong cua class
            //Xac dinh method
            return new MsgContent();
        }
    }
}